<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="优惠券编号">
            <a-input v-decorator="['couponNo']" placeholder="请输入优惠券编号"/>
          </a-form-item>
          <a-form-item label="优惠券名称">
            <a-input v-decorator="['couponName']" placeholder="请输入优惠券名称"/>
          </a-form-item>
          <a-form-item label="领取时间">
            <a-range-picker v-decorator="['between']" format="YYYY-MM-DD HH:mm:ss" show-time
                            value-format="YYYY-MM-DD HH:mm:ss"/>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>

      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <div class="tab-list fl-l">
          <a-radio-group :defaultValue="queryParam.usedTag" @change="handleTabs">
            <a-radio-button v-for="(item, index) in UsedTagEnum.data" :key="index" :value="item.key">
              {{ item.name }}
            </a-radio-button>
          </a-radio-group>
        </div>
      </div>
    </div>
    <s-table
        ref="table"
        rowKey="code"
        :loading="isLoading"
        :columns="columns"
        :data="loadData"
        :pageSize="15"
    >
      <!-- 会员信息 -->
      <template v-slot:user="text, item">
        <UserItem v-if="text" :user="text"/>
        <div v-else>{{ item.userId }}</div>
      </template>
      <!-- 优惠券类型 -->
      <template v-slot:couponType="text">
        <a-tag>{{ CouponTypeEnum[text].name }}</a-tag>
      </template>
      <!-- 最低消费金额 -->
      <template v-slot:full="text">
        <p class="c-p">{{ text }}</p>
      </template>
      <!-- 优惠方式 -->
      <template v-slot:discount="item">
        <template v-if="item.couponType == CouponTypeEnum.REDUCTION.key">
          <span>立减</span>
          <span class="c-p mlr-2">{{ item.amount }}</span>
          <span>元</span>
        </template>
        <template v-if="item.couponType == CouponTypeEnum.DISCOUNT.key">
          <span>打</span>
          <span class="c-p mlr-2">{{ item.discount }}</span>
          <span>折</span>
        </template>
      </template>
      <!-- 有效期 -->
      <template v-slot:useTime="item">
        <p class="c-p">{{ item.useStart | dayjs }} ~ {{ item.useEnd | dayjs }}</p>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/market/coupon'
import {STable, UserItem} from '@/components/Table'
import {CouponTypeEnum, UsedTagEnum} from '@/common/enum/coupon'

export default {
  name: 'Index',
  components: {
    STable,
    UserItem
  },
  data() {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {
        usedTag: UsedTagEnum.UNUSED.key, // 默认查询未使用的优惠券
      },
      // 枚举类
      CouponTypeEnum,
      UsedTagEnum,
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '会员信息',
          dataIndex: 'user',
          scopedSlots: {customRender: 'user'}
        },
        {
          title: '优惠券名称',
          dataIndex: 'couponName'
        },
        {
          title: '优惠券类型',
          dataIndex: 'couponType',
          scopedSlots: {customRender: 'couponType'}
        },
        {
          title: '满减条件',
          dataIndex: 'full',
          scopedSlots: {customRender: 'full'}
        },
        {
          title: '优惠方式',
          scopedSlots: {customRender: 'discount'}
        },
        {
          title: '有效期',
          scopedSlots: {customRender: 'useTime'}
        },
        {
          title: '领取时间',
          dataIndex: 'receiveTime'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.receive({...param, ...this.queryParam})
            .then(response => {
              return response
            })
      }
    }
  },
  created() {

  },
  methods: {
    // 切换tab
    handleTabs(e) {
      this.queryParam.usedTag = e.target.value
      this.handleRefresh(true)
    },

    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = {...this.queryParam, ...values}
          this.handleRefresh(true)
        }
      })
    }

  }
}
</script>
